package shark.picture.controller;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Slf4j
public class MMM {
    public static void main(String[] args) throws IOException {
        String inputPdfPath = "E:\\shark\\需求文档\\2025-04-29 直通车\\文件汇总\\业扩直通车-四类单据模板\\业务费缴费通知单模板.pdf";
        String outputPdfPath = "E:\\shark\\需求文档\\2025-04-29 直通车\\文件汇总\\业扩直通车-四类单据模板\\业务费缴费通知单5.pdf";
        String imagePath = "E:\\shark\\需求文档\\2025-04-29 直通车\\文件汇总\\流程单据PDF\\现场勘查单\\th.jpg";

        // 插入位置坐标（左下角为原点）
        float x = 195; // 距离左边的距离
        float y = 348; // 距离底部的距离
        // 图片尺寸
        float width = 55; // 图片宽度
        float height = 28; // 图片高度
        // 旋转角度（顺时针，以度为单位）
        try (PDDocument document = PDDocument.load(new File(inputPdfPath))) {
            // 旋转签名图片

            byte[] bytes = FileUtils.readFileToByteArray(new File(imagePath));
            BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(bytes));

            double radians = Math.toRadians(0);
            double sin = Math.abs(Math.sin(radians));
            double cos = Math.abs(Math.cos(radians));
            int newWidth = (int) Math.round(originalImage.getWidth() * cos + originalImage.getHeight() * sin);
            int newHeight = (int) Math.round(originalImage.getWidth() * sin + originalImage.getHeight() * cos);

            BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, originalImage.getType());
            Graphics2D g2d = rotatedImage.createGraphics();

            AffineTransform transform = new AffineTransform();
            transform.translate((newWidth - originalImage.getWidth()) / 2.0, (newHeight - originalImage.getHeight()) / 2.0);
            transform.rotate(radians, originalImage.getWidth() / 2.0, originalImage.getHeight() / 2.0);
            g2d.setTransform(transform);

            g2d.drawImage(originalImage, 0, 0, null);
            g2d.dispose();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(rotatedImage, "png", outputStream);
            byte[] byteArray = outputStream.toByteArray();
            PDPage page = document.getPage(0);

            // 加载图片
            PDImageXObject image = PDImageXObject.createFromByteArray(document, byteArray, "业务费缴费通知单");

            // 在指定位置插入图片
            try (PDPageContentStream contentStream = new PDPageContentStream(
                    document, page,
                    PDPageContentStream.AppendMode.APPEND, // 追加模式
                    true,  // 压缩
                    true)) { // 重置上下文

                // 绘制图片（x, y是图片左下角坐标）
                contentStream.drawImage(image, x, y, width, height);
            }
            document.save(outputPdfPath);
        } catch (IOException e) {
            log.info("图片插入失败" , e);
        }
    }
}
